package operation.modules.flashData.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import operation.modules.flashData.dao.FlashDataDao;
import operation.modules.flashData.dto.FlashDTO;
import operation.modules.flashData.entity.FlashDataEntity;
import operation.modules.flashData.service.FlashDataService;
import operation.util.ParamUtil;
import operation.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("flashDataService")
public class FlashDataServiceImpl extends ServiceImpl<FlashDataDao, FlashDataEntity> implements FlashDataService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FlashDataService flashDataService;

    /**
     * 查询数据库所有表
     */
    public List<FlashDataEntity> query(Map<String, Object> params) {
        int id = ParamUtil.getInt(params, "id");
        String name = ParamUtil.getString(params, "name");
        String comment = ParamUtil.getString(params, "comment");
        String data = ParamUtil.getString(params, "data");
        int parentId = ParamUtil.getInt(params, "parent_id");
        QueryWrapper<FlashDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id > 0, "id", id);
        queryWrapper.eq(StringUtils.isNotEmpty(name), "name", name);
        queryWrapper.eq(StringUtils.isNotEmpty(comment), "comment", comment);
        queryWrapper.eq(StringUtils.isNotEmpty(data), "data", data);
        queryWrapper.eq(parentId >= 0, "parent_id", parentId);
        List<FlashDataEntity> allDataList = baseMapper.query(queryWrapper);
        System.out.println("查询结果:" + allDataList);
        return allDataList;
    }

    /**
     * 查询所有数据,返回数据结构
     */
    public List<FlashDTO> queryAll() {
        // 查询所有数据条数
        List<FlashDTO> flashDTOS = baseMapper.queryAll();
        List<FlashDTO> flashList = new ArrayList<>();
        if (flashDTOS.size() > 0) {
            for (int i = 0; i < flashDTOS.size(); i++) {
                if (flashDTOS.get(i).getParentId() == 0) {
                    flashList.add(flashDTOS.get(i));
                }
            }
            // 删除已转移的数据
            flashDTOS.removeAll(flashList);
        } else {
            return new ArrayList<>();
        }

        flashList = translateFlashDTO(flashList, flashDTOS);

        return flashList;
    }

    /**
     * 实体转换
     */
    private List<FlashDTO> translateFlashDTO (List<FlashDTO> newDTO, List<FlashDTO> oldDTO) {
        if (newDTO != null) {
            for (int i = 0; i < newDTO.size(); i++) {
                List<FlashDTO> removeDTO = new ArrayList<>();
                List<FlashDTO> child = new ArrayList<>();
                for (int j = 0; j < oldDTO.size(); j++) {
                    if (newDTO.get(i).getValue() == oldDTO.get(j).getParentId()) {
                        child.add(oldDTO.get(j));
                        removeDTO.add(oldDTO.get(j));
                    }
                }
                if (child != null && child.size() > 0) {
                    newDTO.get(i).setChildren(child);
                }
                oldDTO.removeAll(removeDTO);
                for (int m = 0; m < 10; m++) {
                    translateFlashDTO(newDTO.get(i).getChildren(), oldDTO);
                }
            }
        }
        return newDTO;
    }

    /**
     * 根据id查询
     */
    public FlashDataEntity queryById(Map<String, Object> params) {
        int id = ParamUtil.getInt(params, "id");
        FlashDataEntity flashDataEntity = baseMapper.selectById(id);
        String[] typeList = {"", "warning", "success", "info", "danger"};
        flashDataEntity.setStyleType(typeList[(int)(Math.random() * typeList.length)]);
        return flashDataEntity;
    }

}
